﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DIST.LR.ZHJG.Events;
using DIST.LR.ZHJG.DataModel;
using System.Collections.ObjectModel;
using DIST.LR.ZHJG.WCFServices.EntityDataManageService;
using System.ComponentModel;

namespace DIST.LR.ZHJG.Pgybc
{
    public partial class Zbgl_View : UserControl
    {
        protected EntityDataManageServiceClient client = DIST.LR.ZHJG.WCFServices.CommonServices.WCFService;

        public Zbgl_View()
        {
            InitializeComponent();
            //接收消息
            EventAggregatorRepository
                .EventAggregator.GetEvent<RadTreeSelectChangeEvent>()
                .Subscribe(TreeSelectChangeEvent);
        }

        KPI _lstKPI;
        IList<TKPIVALUE> _lstKPIValue;
        IList<TKPIORIGIN> _lstKPIOrigin;
        public void TreeSelectChangeEvent(KPI kpiModel)
        {
            _lstKPI = kpiModel;
            GetKPIOriginMonitorData(kpiModel.KPIOriginid);
        }

        public void GetKPIOriginMonitorData(int KPIOriginid)
        {
            try
            {
                string strSql = string.Format("from TKPIORIGIN where ID=" + KPIOriginid);
                client.SelectTkpiOriginCompleted += new EventHandler<SelectTkpiOriginCompletedEventArgs>(client_SelectTkpiOriginCompleted);
                client.SelectTkpiOriginAsync(strSql, "0");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void client_SelectTkpiOriginCompleted(object sender, SelectTkpiOriginCompletedEventArgs e)
        {
            _lstKPIOrigin = e.Result as IList<TKPIORIGIN>;
            GetKPIValueMonitorData(_lstKPI.KPIId);
        }

        public void GetKPIValueMonitorData(int KPIid)
        {
            try
            {
                string strSql = string.Format("from TKPIVALUE where KPIID=" + KPIid);
                client.SelectTkpiValueCompleted += new EventHandler<SelectTkpiValueCompletedEventArgs>(client_SelectTkpiValueCompleted);
                client.SelectTkpiValueAsync(strSql, "0");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void client_SelectTkpiValueCompleted(object sender, SelectTkpiValueCompletedEventArgs e)
        {
            _lstKPIValue = e.Result as IList<TKPIVALUE>;
            GetViewDataSource(_lstKPI, _lstKPIValue, _lstKPIOrigin);
        }

        ViewDataSource Vd;
        KPIViewData kpiViewData;
        /// <summary>
        /// 构造详细信息展示数据源
        /// </summary>
        /// <param name="lstKPI">指标配置</param>
        /// <param name="lstKPIValue">指标值</param>
        /// <param name="lstKPIOrigin">指标值来源</param>
        public void GetViewDataSource(KPI lstKPI, IList<TKPIVALUE> lstKPIValue, IList<TKPIORIGIN> lstKPIOrigin)
        {
            Vd = new ViewDataSource();
            kpiViewData = new DataModel.KPIViewData();
            kpiViewData.KPIDescption = lstKPI.KPIDescption;
            kpiViewData.KPIName = lstKPI.KPIName;
            kpiViewData.KPIUnit = lstKPI.KPIUnit;
            if (lstKPIValue == null)
            {
                return;
            }
            else
            {
                foreach (TKPIVALUE item in lstKPIValue)
                {
                    kpiViewData.KPIVALUEEndTime = item.ENDTIME;
                    kpiViewData.KPIVALUEId = item.ID;
                    kpiViewData.KPIVALUEKpiId = item.KPIID;
                    kpiViewData.KPIVALUEStartTime = item.STARTTIME;
                    kpiViewData.KPIVALUEValue = item.VALUE;
                    kpiViewData.KPIVALUEValueOrigin = item.VALUEORIGIN;
                    kpiViewData.KPIVALUECreateTiem = item.CREATETIME;
                    kpiViewData.KPIVALUEDataTimeType = item.DATETIMETYPE;
                    kpiViewData.KPIVALUEName = item.NAME;
                }
            }
            if (lstKPIOrigin == null)
            {
                return;
            }
            else
            {
                foreach (TKPIORIGIN item in lstKPIOrigin)
                {
                    kpiViewData.KPIORIGINComputeSql = item.COMPUTESQL;
                    kpiViewData.KPIORIGINComputeSql = item.COMPUTESQL;
                    kpiViewData.KPIORIGINConstring = item.CONSTRING;
                    kpiViewData.KPIORIGINGetType = item.GETTYPE;
                    kpiViewData.KPIORIGINId = item.ID;
                    kpiViewData.KPIORIGINName = item.NAME;
                    kpiViewData.KPIORIGINUpdateCycle = item.UPDATECYCLE;
                    kpiViewData.KPIORIGINUpdateTime = item.UPDATETIME;
                    kpiViewData.KPIORIGINValue = item.VALUE;
                }
            }
            Vd.ViewDataSourceAll = kpiViewData;
            this.DataContext = Vd.ViewDataSourceAll;
        }
    }

    public class ViewDataSource : INotifyPropertyChanged
    {
        private KPIViewData viewDataSourceAll;
        public KPIViewData ViewDataSourceAll
        {
            get { return this.viewDataSourceAll; }
            set
            {
                if (this.viewDataSourceAll != value)
                {
                    this.viewDataSourceAll = value;
                    this.OnPropertyChanged("ViewDataSourceAll");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
